package com.web.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Collection;

/**
 * @autor Chen
 * @create 2021--23-13:37
 * @desc
 */
public class DBUtil {
    private final String user = "root";
    private final String pwd = "duanzh";
    private final String url = "jdbc:mysql://localhost:3306/temperature?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8";
    private final String driver = "com.mysql.cj.jdbc.Driver";

    private static DBUtil dbUtil =new DBUtil();

    private Connection connection =null;

    private DBUtil() {
    }

    public static DBUtil getInstance() {
        return dbUtil;
    }
    public Connection getConnection() throws Exception{
        Class.forName(driver);
        connection = DriverManager.getConnection(url, user, pwd);
        return connection;

    }
    public void close() {
        try {
            if (connection != null && !connection.isClosed()) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
